/*
 * Copyright (C) 2015 Federico Iosue (federico.iosue@gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.yitongmao.common.reminder;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.yitongmao.common.UrlConstants;
import com.yitongmao.yitmfruit.R;


public class AlarmReceiver extends BroadcastReceiver {

    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder mBuilder;

    @Override
    public void onReceive(Context mContext, Intent intent) {
        try {
            ReminderBean reminderBean = (ReminderBean) intent.getExtras().getSerializable(UrlConstants.INTENT_NOTE);
            if (reminderBean != null) {
                createNotification(mContext, reminderBean);
                ReminderHelper.removeReminder(mContext, reminderBean);
            }
        } catch (Exception e) {
            Log.e("AlarmReceiver", "Error on receiving reminder", e);
        }
    }

    private void createNotification(Context context, ReminderBean reminderBean) {

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent tuiIntent;
        Bundle tuibundle = new Bundle();
//		tuibundle.putString("getuiData", data);
//		tuiIntent = new Intent(context, ReceiveGeTuiData.class);
//		tuiIntent.putExtras(tuibundle);
//		tuiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		PendingIntent contentIntent = PendingIntent.getService(context, articleID, tuiIntent,
//				PendingIntent.FLAG_CANCEL_CURRENT);
        // 创建Notifcation

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setContentTitle("直播即将开始")
                .setContentText(reminderBean.getTitle());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//悬挂式Notification，5.0后显示
//                                    mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
            mBuilder.setSmallIcon(R.mipmap.ic_launcher);// 设置通知小ICON（5.0必须采用白色透明图片）
            mBuilder.setColor(context.getResources().getColor(R.color.colorPrimaryDark));
        } else {
            mBuilder.setSmallIcon(R.mipmap.ic_launcher);// 设置通知小ICON
        }
        mBuilder.setTicker(context.getResources().getString(R.string.app_name));
        mBuilder.setWhen(System.currentTimeMillis());
        mBuilder.setAutoCancel(true);
//		mBuilder.setContentIntent(contentIntent);
        NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle();
        textStyle.setBigContentTitle(context.getResources().getString(R.string.app_name));
        textStyle.bigText(reminderBean.getTitle());
        mBuilder.setStyle(textStyle);
        textStyle.setBuilder(mBuilder);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            // 5.0悬浮通知
            mBuilder.setPriority(Notification.PRIORITY_MAX);
            mBuilder.setVibrate(new long[0]);
        }
        // mBuilder.setContentText(title);
        // RemoteViews mContentView = new RemoteViews(
        // context.getPackageName(),
        // R.layout.getui_notify);
        // mContentView.setImageViewResource(R.id.img_icon,
        // R.drawable.icon);
        // mContentView.setTextViewText(R.id.txt_geTui_title,
        // title);
        // mContentView.setTextViewText(
        // R.id.txt_geTui_content, content);
        // mBuilder.setContent(mContentView);
        mBuilder.setDefaults(
                Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
        mNotificationManager.notify(reminderBean.getFileID(), mBuilder.build());

    }
}
